package main

import (
	"strconv"
	"github.com/boltdb/bolt"
	"github.com/xuri/excelize"
	"match-spider/util/boltdb"
	"strings"
	"fmt"
	"math"
	"match-spider/util/format"
	"regexp"
)

func checkAsiaScore2018(data string) (bool,[]float64) {
	asiaSlice:=strings.Split(data,"!")

	//开盘公司至少10家
	size:=len(asiaSlice)
	if size<10{
		return false,nil
	}
	//初盘盘口-0.25~0.25
	i:=int(0)
	j:=int(0)
	m:=int(0)
	len:=int(0)
	//初盘均值
	s1:=float64(0)
	//终盘均值
	s2:=float64(0)
	oddsData:=make(map[string][]string)
	asia0:=make(map[string]int64)
	asia:=make(map[string]int64)
	a:=float64(-9999)
	b:=float64(-9999)
	c:=float64(-9999)


	for _,v:=range asiaSlice{
		asiaOdds:=strings.Split(v,"^")
		name:=asiaOdds[0]
		if name=="Crown"||name=="ＳＢ"{
			a,_=strconv.ParseFloat(asiaOdds[5],64)
			b,_=strconv.ParseFloat(asiaOdds[6],64)
			c,_=strconv.ParseFloat(asiaOdds[7],64)
		}

		odds,_:=strconv.ParseFloat(asiaOdds[3],64)
		odds2,_:=strconv.ParseFloat(asiaOdds[6],64)
		s1=s1+odds
		s2=s2+odds2
		if math.Abs(odds)!=1&&math.Abs(odds)!=0.75&&math.Abs(odds)!=0.5{
			i++
		}
		if odds==odds2&&odds2==0{
			//初盘，终盘不能全都是0盘口不变化
			j++
		}
		if math.Abs(odds2)!=1&&math.Abs(odds2)!=0.75&&math.Abs(odds2)!=0.5{
			//终盘不在范围的去掉
			m++
		}
		//以终盘中大众盘口作为最终终盘口
		if asia[asiaOdds[6]]==0{
			asia[asiaOdds[6]]=1
		}else{
			asia[asiaOdds[6]]=asia[asiaOdds[6]]+1
		}
		if asia0[asiaOdds[3]]==0{
			asia0[asiaOdds[3]]=1
		}else{
			asia0[asiaOdds[3]]=asia0[asiaOdds[3]]+1
		}
		oddsData[name]=asiaOdds
		len++
	}


	//初盘
	n0:=float64(-9999)
	temp0:=int64(0)
	//终盘
	n:=float64(-9999)
	temp:=int64(0)
	for k,v:=range asia0{
		if v>temp0{
			temp0=v
			n0,_=strconv.ParseFloat(k,64)

		}
	}
	for k,v:=range asia{
		if v>temp{
			temp=v
			n,_=strconv.ParseFloat(k,64)

		}
	}
	if math.Abs(n)>1{
		return false,nil
	}
	return true,[]float64{
		a,b,c,n0,n,s1/float64(len),s2/float64(len),
	}
}
const ll="data"
func score2018(fn func([]interface{},int)){
	//only:=",中超,丹麦甲,乌克超,阿联酋超,澳维超,法甲,土甲,土超,巴西甲,意乙,捷U19,法乙,泰甲," +
	//	"瑞典甲,瑞典超,白俄超,突尼甲,苏冠,英议南,英冠,荷甲,"
	//only:=",法甲,法乙,英冠,英超,英乙,英甲,德甲,德乙,西甲,西乙,意甲,意乙,苏冠,荷甲,荷乙,葡超,"
	/*leagueType:=map[string]int{
		"中超":0,
		"丹麦甲":1,
		"乌克超":2,
		"阿联酋超":3,
		"澳维超":4,
		"法甲":5,
		"土甲":6,
		"土超":7,
		"巴西甲":8,
		"意乙":9,
		"捷U19":10,
		"法乙":11,
		"泰甲":12,
		"荷甲":13,
		"瑞典甲":14,
		"瑞典超":15,
		"白俄超":16,
		"突尼甲":17,
		"苏冠":18,
		"英议南":19,
		"英冠":20,
	}*/
	i:=0
	boltdb.ScanTx("matchs-2017", func(tx *bolt.Tx) {

		tx.ForEach(func(name []byte, b *bolt.Bucket) error {
			b.ForEach(func(k, v []byte) error {
				content:=string(v)
				s:=strings.Split(content,"^")

				league:=s[1]
				startAt:=s[2]
				reg, _ := regexp.Compile("\\([\\S\\s]+?\\)")
				t1:=s[3]
				t2:=s[5]
				t1= reg.ReplaceAllString(t1, "")
				t2= reg.ReplaceAllString(t2, "")
				s1,_:=strconv.ParseFloat(s[4],64)
				s2,_:=strconv.ParseFloat(s[6],64)

				/*if !strings.Contains(only,","+league+",")||strings.Contains(league,"杯"){
					return nil
				}*/

				if strings.Contains(league,"友谊")||strings.Contains(league,"杯"){
					return nil
				}
				boltdb.Get("asia-2017",league,string(k), func(asia string) {

					e, pk := checkAsiaScore2018(asia)
					if (!e) {
						return
					}
					boltdb.Get("europe-2017", league, string(k), func(europe string) {
						boltdb.Get("lastmatch-2017", league, string(k), func(text string) {
							//去掉没有联赛的比赛
							if !strings.Contains(text, "总") || !strings.Contains(text, "BaseInfo") {
								return
							}
							boltdb.Get("bf-2017", league, string(k), func(bf string) {
								boltdb.Get("airlive-2017", league, string(k), func(live string) {

									y, ff := format.FormatData(string(k), league, startAt, t1, t2, text, europe, bf,live)

									if y {

										w := 0

										if (s1 - s2 ) == 0 {
											w = 1
										}
										if (s1 - s2 ) < 0 {
											w = 2
										}

										slice := []interface{}{string(k),s1-s2,w,pk[0],pk[1],pk[2],pk[3],pk[4],league}
										for _, v := range ff {
											for _, vv := range v {
												slice = append(slice, vv)
											}

										}

										fmt.Println(i)
										fn(slice, i)
										i++
									}
								})
							})
						})
					})
				})

				return nil
			})
			return nil
		})

	})
	fmt.Println(i)
}

func main()  {
	e:=[]string{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}
	xlsx := excelize.NewFile()

	categories := []string{

	}
	for k,v:=range categories{
		len:=int(k/26)
		s:=""
		if len>0{
			s+=e[len-1]+e[k%26]
		}else{
			s+=e[k%26]
		}

		xlsx.SetCellValue("Sheet1",s+"1", v)
	}
	score2018(func(s []interface{}, i int) {
		for k,v:=range s{
			len:=int(k/26)
			s:=""
			if len>0{
				s+=e[len-1]+e[k%26]
			}else{
				s+=e[k%26]
			}
			xlsx.SetCellValue("Sheet1",s+strconv.Itoa(i+1), v)
		}
	})
	err := xlsx.SaveAs("./out/"+ll+".xlsx")
	if err != nil {
		fmt.Println(err)
	}

}

